const path = require('path')
const webpack = require('webpack');
// 自动在dist生成html，JS文件
const HtmlWebpackPlugin = require('html-webpack-plugin')
// 每次执行前清空dist目录
const CleanWebpackPlugin = require('clean-webpack-plugin')
const VueLoaderPlugin = require('vue-loader/lib/plugin')
module.exports = {
  entry: {
    electron: path.resolve(__dirname, 'src/entry/main.ts'),
    service: path.resolve(__dirname, 'src/entry/service.ts')
  },
  output: {
    filename: '[name].bundle.js',
    path: path.resolve(__dirname, 'dist'),
    // chunkFilename路径将会作为组件懒加载的路径
    chunkFilename: '[name].bundle.js',
    // 线上路径或者 cdn 路径
    publicPath: '/'
  },
  resolve: {
    extensions: ['.js', '.vue', '.json', '.ts'],
    alias: {
      '@': path.resolve('src'),
      'router': path.resolve(__dirname, 'src/router'),
      'pages': path.resolve(__dirname, 'src/pages'),
      'utils': path.resolve(__dirname, 'src/utils'),
      'store': path.resolve(__dirname, 'src/store'),
      'asset': path.resolve(__dirname, 'src/asset'),
    }
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        use: [
          'vue-loader'
        ]
      },
      {
        test: /\.ts$/,
        exclude: /node_modules/,
        enforce: 'pre',
        loader: 'tslint-loader'
      },
      {
        test: /\.tsx?$/,
        loader: 'ts-loader',
        exclude: /node_modules/,
        options: {
          appendTsSuffixTo: [/\.vue$/],
        }
      },
      {
        test: /\.css$/,
        use: [
          // 注意style-loader跟css-loader的路径
          'vue-style-loader',
          'style-loader',
          'css-loader'
        ]
      },
      {
        test: /\.(png|svg|jpg|gif)$/,
        use: [
          'file-loader'
        ]
      },
      {
        test: /\.(woff|woff2|eot|ttf|otf)$/,
        use: [
          'file-loader'
        ]
      }
    ]
  },
  plugins: [
    new CleanWebpackPlugin.CleanWebpackPlugin({ verbose: true }),
    new HtmlWebpackPlugin({
      title: 'Electron', // 将来将会用模板语法插入的页面title
      filename: __dirname + '/dist/index.html', // build过后的文件路径及地址
      chunks: ['electron'], // 其实时入口文件
      template: path.resolve(__dirname, 'mainPages/index.html') // 模板语法将在哪个模板中应用
    }),
    // 每多一个页面，就得多一个HtmlWebpackPlugin
    new HtmlWebpackPlugin({
      title: 'Service',
      filename: __dirname + '/dist/service.html',
      template: path.resolve(__dirname, 'mainPages/service.html'),
      chunks: ['service']
    }),
    new webpack.ProvidePlugin({
      // axios: 'axios'
    }),
    new VueLoaderPlugin()
  ]
}